﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;

/// <summary>
/// Summary description for Vendor
/// </summary>
/// 
namespace LinqObj
{
    public partial class Vendor_Detail
    {
        public string VendorDetail
        {
            get
            {
                string detail = this.given_name + " " + this.family_name + "<br/>" +
                                    this.post_address2 + ", " + this.post_address1 + "<br/>" +
                                    this.post_suburb + " " + this.post_state + " " + this.post_code;
                return detail;

            }
        }

        private static ToolboxDataContext _context = new ToolboxDataContext();

        public static void AadVendor(Vendor_Detail _vendor)
        {
            _context.GetTable<Vendor_Detail>().InsertOnSubmit(_vendor);
            try
            {
                _context.SubmitChanges();
            }
            catch (Exception ex)
            {
                _context.ChangeConflicts.Clear();
                _context.SubmitChanges();
            }
        }

        public static Vendor_Detail GetVendorByProperty(string strPropertyID)
        {
            int propertyID = 0;
            Int32 val;
            if (Int32.TryParse(strPropertyID, out val))
                propertyID = Convert.ToInt32(strPropertyID);
            else
                return null;
            return (from _property in _context.GetTable<Property>()
                    where _property.property_id == propertyID
                    select _property).Single().Vendor_Detail;
        }
    }
}
    //    private string givenName;
    //    private string familyName;
    //    private string address1;
    //    private string address2;
    //    private int propertyID;
    //    private string state;
    //    private string postcode;
    //    private string email;
    //    private string contactNumber;
    //    private string mobileNumber;
    //    private int vendorID;
    //    private string suburb;    

    //    public string GiveName { set { this.givenName = value; } get { return this.givenName; } }
    //    public string FamilyName { set { this.familyName = value; } get { return this.familyName; } }
    //    public string Email { set { this.email = value; } get { return this.email; } }
    //    public int PropertyID { set { this.propertyID = value; } get { return this.propertyID; } }
    //    public string State { set { this.state = value; } get { return this.state; } }
    //    public string MobileNumber { set { this.mobileNumber = value; } get { return this.mobileNumber; } }
    //    public string Address1 { set { this.address1 = value; } get { return this.address1; } }
    //    public string Address2 { set { this.address2 = value; } get { return this.address2; } }
    //    public int VendorID { set { this.vendorID = value; } get { return this.vendorID; } }
    //    public string Postcode { set { this.postcode = value; } get { return this.postcode; } }
    //    public string Suburb { set { this.suburb = value; } get { return this.suburb; } }
    //    public string ContactNumber { set { this.contactNumber = value; } get { return this.contactNumber; } }

    //    /*
    //     * Vendor Name and Post Address
    //     */ 
    //    public string VendorDetail
    //    {
    //        get
    //        {
    //            string detail = this.GiveName + " " + this.FamilyName + "<br/>" +
    //                                this.Address1 + ", " + this.Address2 + "<br/>" +
    //                                this.Suburb + " " + this.State + " " + this.Postcode;
    //            return detail;

    //        }
    //    }

    //    public Vendor()
    //    {

    //    }

    //    public static void SaveVendor(IDbConnection conn, Vendor vendor)
    //    {
    //        if (vendor.PropertyID > 0)
    //            updateVendor(conn, vendor);
    //        else
    //            insertVendor(conn, vendor);
    //    }

    //    public static Vendor GetVendorByVendor(IDbConnection conn, int vendorID)
    //    {
    //        return null;
    //    }

    //    public static Vendor GetVendorByProperty(IDbConnection conn, int propertyID)
    //    {
    //        string sql_script = @"select vendor_id, given_name, family_name, email, contact_number,
    //                                post_address1, post_address2, post_state, post_code, mobile_number                                
    //                            from vendor_detail where property_id = " + propertyID;

    //        IDbCommand cmd = conn.CreateCommand();
    //        cmd.CommandText = sql_script;

    //        IDataReader dr = cmd.ExecuteReader();
    //        Vendor vendor = new Vendor();

    //        if (dr.Read())
    //        {
    //            vendor.vendorID = Convert.ToInt32(dr["vendor_id"].ToString());
    //            vendor.givenName = dr["given_name"].ToString();
    //            vendor.familyName = dr["family_name"].ToString();
    //            if (!dr.IsDBNull(3))
    //                vendor.email = dr["email"].ToString();
    //            if (!dr.IsDBNull(4))
    //                vendor.contactNumber = dr["contact_number"].ToString();
    //            if (!dr.IsDBNull(5))
    //                vendor.Address1 = dr["post_address1"].ToString();
    //            if (!dr.IsDBNull(6))
    //                vendor.Address2 = dr["post_address2"].ToString();
    //            if (!dr.IsDBNull(7))
    //                vendor.state = dr["post_state"].ToString();
    //            if (dr.IsDBNull(8))
    //                vendor.postcode = dr["post_code"].ToString();
    //            if (dr.IsDBNull(9))
    //                vendor.mobileNumber = dr["mobile_number"].ToString();
    //        }

    //        return vendor;
    //    }

    //    private static bool updateVendor(IDbConnection conn, Vendor vendor)
    //    {
    //        string sql_script = @"update [Vendor_Detail]
    //                            set given_name = @givenName, family_name = @familyName, email = @email, 
    //                                contact_number = @contactNumber, post_address1 = @postAddress1,
    //                                post_address2 = @postAddress2, post_state = @postState, post_code = @postCode, 
    //                                mobile_number = @mobileNumber
    //                            where property_id = @propertyID";

    //        IDbTransaction tran = conn.BeginTransaction();
    //        try
    //        {
    //            IDbCommand cmd = conn.CreateCommand();
    //            cmd.CommandText = sql_script;
    //            IDbDataParameter param = cmd.CreateParameter();
    //            param.ParameterName = "@givenName";
    //            param.DbType = DbType.String;
    //            param.Value =  SetDbValue(vendor.givenName);
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@familyName";
    //            param.DbType = DbType.String;
    //            param.Value = SetDbValue(vendor.familyName);
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@email";
    //            param.DbType = DbType.String;
    //            param.Value = SetDbValue(vendor.email);
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@contactNumber";
    //            param.DbType = DbType.String;
    //            param.Value = SetDbValue(vendor.contactNumber);
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@postAddress1";
    //            param.DbType = DbType.String;
    //            param.Value = SetDbValue(vendor.address1);
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.DbType = DbType.String;
    //            param.ParameterName = "@postAddress2";
    //            param.Value = SetDbValue(vendor.address2);
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.DbType = DbType.String;
    //            param.ParameterName = "@postCode";
    //            param.Value = SetDbValue(vendor.Postcode);
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.DbType = DbType.String;
    //            param.ParameterName = "@mobileNumber";
    //            param.Value = SetDbValue(vendor.mobileNumber);
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.DbType = DbType.String;
    //            param.ParameterName = "@postState";
    //            param.Value = SetDbValue(vendor.state);
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.DbType = DbType.Int32;
    //            param.ParameterName = "@propertyID";
    //            param.Value = SetDbValue(vendor.propertyID);            
    //            cmd.Parameters.Add(param);

    //            cmd.Transaction = tran;
    //            cmd.ExecuteNonQuery();
    //            tran.Commit();
    //            return true;
    //        }
    //        catch (Exception error)
    //        {
    //            tran.Rollback();
    //            throw (error);
    //        }
    //    }

    //    private static bool insertVendor(IDbConnection conn, Vendor vendor)
    //    {
    //        string sql_script = @"insert into vendor(given_name, family_name, email, contact_number, post_address1,
    //                                post_address2, post_state, post_code, mobile_number, property_id)
    //                                values(@given_name, @familyName, @email, @contactNumber, @postAddress1, @postAddress2,
    //                                @postCode, @mobileNumber, @propertyID)";

    //        IDbTransaction tran = conn.BeginTransaction();
    //        try
    //        {
    //            IDbCommand cmd = conn.CreateCommand();
    //            cmd.CommandText = sql_script;
    //            IDbDataParameter param = cmd.CreateParameter();
    //            param.ParameterName = "@given_name";
    //            param.DbType = DbType.String;
    //            param.Value = vendor.givenName;
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@familyName";
    //            param.DbType = DbType.String;
    //            param.Value = vendor.familyName;
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@email";
    //            param.Value = vendor.email;
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@contactNumber";
    //            param.Value = vendor.contactNumber;
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@postAddress1";
    //            param.Value = vendor.address1;
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@postAddress2";
    //            param.Value = vendor.address2;
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = " @postCode";
    //            param.Value = vendor.Postcode;
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@contactNumber";
    //            param.Value = vendor.contactNumber;
    //            cmd.Parameters.Add(param);

    //            param = cmd.CreateParameter();
    //            param.ParameterName = "@propertyID";
    //            param.Value = vendor.contactNumber;
    //            cmd.Parameters.Add(param);

    //            cmd.Transaction = tran;
    //            cmd.ExecuteNonQuery();

    //            cmd.CommandText = @"select IDENT_CURRENT('vendor') as vendorID";

    //            int vendorID = Convert.ToInt32(cmd.ExecuteScalar().ToString());
    //            tran.Commit();
    //            if (vendorID > 0)
    //            {
    //                vendor.vendorID = vendorID;
    //                return true;
    //            }
    //            return false;
    //        }
    //        catch (Exception error)
    //        {
    //            tran.Rollback();
    //            throw (error);
    //        }

    //    }